package com.example.villagemanager.ui.activity;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;

import com.example.villagemanager.R;
import com.example.villagemanager.listener.OnItemClickListener;
import com.example.villagemanager.model.bean.MainMenu;
import com.example.villagemanager.model.bean.Menu;
import com.example.villagemanager.model.entity.BaseResponse;
import com.example.villagemanager.model.event.Event;
import com.example.villagemanager.network.BaseObserver;
import com.example.villagemanager.network.RetrofitHelper;
import com.example.villagemanager.network.RxUtil;
import com.example.villagemanager.network.api.AppInfoApi;
import com.example.villagemanager.ui.adapter.MainMenuAdapter;
import com.example.villagemanager.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {
    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;
    List<MainMenu> mainMenus=new ArrayList<>();
    MainMenuAdapter mainMenuAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initActivityData() {
        httpGetMenuList();

    }
    private void httpGetMenuList() {
        RetrofitHelper.create(AppInfoApi.class)
                .menuList(1,0)
                .compose(RxUtil.observervableToMain())
                .as(RxUtil.bindLifecycle( this))
                .subscribe(new BaseObserver<List<Menu>>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<List<Menu>> response) {
                        if (response.getData()!=null) {
                            for(Menu menu:response.getData()){
                                MainMenu mainMenu=new MainMenu();
                                mainMenu.setTitle(menu.getLabel());
                                mainMenu.setId(menu.getValue().intValue());
                                mainMenus.add(mainMenu);

                            }
                            mainMenuAdapter.notifyDataSetChanged();




                        }
                    }
                });
    }

    @Override
    protected void initContentView() {

        mainMenuAdapter=new MainMenuAdapter(getContext(),mainMenus);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),5));
        recyclerView.setAdapter(mainMenuAdapter);
        mainMenuAdapter.setmOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (mIntent==null){
                    mIntent=new Intent();
                }
                mBundle=mIntent.getExtras();
                if (mBundle==null){
                    mBundle=new Bundle();
                }
                mBundle.putInt("MENUID",mainMenus.get(position).getId());
                sendIntent(MemuActivity.class,mBundle);

            }
        });

    }

    @Override
    protected int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void onEventProcess(Event event) {

    }
}